package com.example.turismo_pucela;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;

import org.xmlpull.v1.XmlSerializer;

import android.content.Context;
import android.util.Xml;

public class EscribirXML {

	public void escribirLugares(Context context, String fichero, ArrayList<Lugar> lugares) throws IllegalArgumentException, IllegalStateException, IOException {
		 	
			FileOutputStream fileos = context.openFileOutput(fichero, Context.MODE_PRIVATE);
		    XmlSerializer xmlSerializer = Xml.newSerializer();              
		    StringWriter writer = new StringWriter();
		    
		    	xmlSerializer.setOutput(writer);
		    	xmlSerializer.startDocument("UTF-8", true);
		    	xmlSerializer.startTag(null, "lugares");
		    	
		    	for (int i = 0; i < lugares.size(); i++) {
		    		Lugar l = lugares.get(i);
		    		xmlSerializer.startTag(null, "lugar");
		    		
			    	xmlSerializer.startTag(null, "id");
			    	xmlSerializer.text(String.valueOf(l.getID()));
			    	xmlSerializer.endTag(null, "id");
			    	
			    	xmlSerializer.startTag(null,"imagen");
			    	xmlSerializer.text(l.getImagen());
			    	xmlSerializer.endTag(null, "imagen");
			    	
			    	xmlSerializer.startTag(null,"nombre");
			    	xmlSerializer.text(l.getNombre());
			    	xmlSerializer.endTag(null, "nombre");
			    	
			    	xmlSerializer.startTag(null,"descripcion");
			    	xmlSerializer.text(l.getDescripcion());
			    	xmlSerializer.endTag(null, "descripcion");
			    	
			    	xmlSerializer.startTag(null, "coordenadas");
				    	xmlSerializer.startTag(null,"x");
				    	xmlSerializer.text(String.valueOf(l.getX()));
				    	xmlSerializer.endTag(null, "x");
			    	
				    	xmlSerializer.startTag(null,"y");
				    	xmlSerializer.text(String.valueOf(l.getY()));
				    	xmlSerializer.endTag(null, "y");
			    	xmlSerializer.endTag(null, "coordenadas");
			    	
			    	xmlSerializer.endTag(null, "lugar");
				}
		    	
		    	xmlSerializer.endTag(null, "lugares");
		    	xmlSerializer.endDocument();
		    	xmlSerializer.flush();
		    String dataWrite = writer.toString();
		    fileos.write(dataWrite.getBytes());
		    fileos.close();
	}
}
